package at.jku.pervasive.simulator;

import java.io.IOException;

import at.jku.pervasive.Measurement;
import at.jku.pervasive.Position;

public class FileServerSimulator extends UbisenseServerSimulator
{

	protected Measurement measurement;
	protected Position[] positions;
	protected int currentPosition;
	private long previousDate;
	private boolean loop;

	public FileServerSimulator(int port, Measurement measurement) throws IOException
	{
		super(port);
		this.loop = true;
		this.measurement = measurement;
		if (this.measurement != null)
			positions = this.measurement.getPositions();
		currentPosition = 0;
	}

	@Override
	protected Position getNextPosition()
	{
		if (currentPosition >= positions.length && loop)
		{
			// reset to start
			currentPosition = 0;
		}

		// get the current position
		Position currentPos = this.positions[this.currentPosition++];
		previousDate = currentPos.getDate().getTime();
		return currentPos;
	}

	@Override
	protected long getWaitingTime()
	{
		if (currentPosition >= positions.length && loop)
			return 0;

		// calculate the time difference between the previous time and the
		// current position
		return Math.abs(this.positions[currentPosition].getDate().getTime() - previousDate);
	}

	public void setLoop(boolean loop)
	{
		this.loop = loop;
	}
}
